/**
 * Copyright 2018 人人开源 http://www.renren.io
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.gson.JsonObject;
import io.renren.common.utils.R;
import io.renren.modules.sys.dao.TokenDao;
import io.renren.modules.sys.entity.TokenEntity;
import io.renren.modules.sys.service.TokenService;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.Calendar;
import java.util.Date;
import java.util.UUID;


@Service("tokenService")
public class TokenServiceImpl extends ServiceImpl<TokenDao, TokenEntity> implements TokenService {
	/**
	 * 12小时后过期
	 */
	private final static int EXPIRE = 3600 * 12;

	@Override
	public TokenEntity queryByToken(String token) {
		return this.selectOne(new EntityWrapper<TokenEntity>().eq("token", token));
	}

	@Override
	public TokenEntity checkToken(long userId) {
		TokenEntity tokenEntity = tokenDao.checkToken(userId);
		return tokenEntity;
	}


	@Override
	public TokenEntity createToken(long userId) {
		//当前时间
		Date now = new Date();
		//过期时间

		Calendar curr = Calendar.getInstance();
		curr.set(Calendar.YEAR,curr.get(Calendar.YEAR)+1);
		Date expireTime = curr.getTime();
	//	Date expireTime = new Date(now.getTime() + EXPIRE * 1000);

		//生成token
		String token = generateToken();

		//保存或更新用户token
		TokenEntity tokenEntity = new TokenEntity();
		tokenEntity.setUserId(userId);
		tokenEntity.setToken(token);
		tokenEntity.setUpdateTime(now);
		tokenEntity.setExpireTime(expireTime);

		/*Jedis jedis = new Jedis("106.14.146.131");
		String tokenStr = jedis.get(""+token);
		JSONObject obj = new JSONObject().fromObject(tokenStr);//将json字符串转换为json对象
		*//*JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"MM-dd-yyyy HH:mm:ss"}) );*//*
		tokenEntity = (TokenEntity) JSONObject.toBean(obj,TokenEntity.class);//将建json对象转换为Person对象
		JSONObject json = JSONObject.fromObject(tokenEntity);
		String jsonStr = json.toString();
		jedis.set(""+token,jsonStr);*/

		this.insertOrUpdate(tokenEntity);

		return tokenEntity;
	}

	@Override
	public void expireToken(long userId){
		Date now = new Date();

		TokenEntity tokenEntity = new TokenEntity();
		tokenEntity.setUserId(userId);
		tokenEntity.setUpdateTime(now);
		tokenEntity.setExpireTime(now);
		this.insertOrUpdate(tokenEntity);
	}

	private String generateToken(){
		return UUID.randomUUID().toString().replace("-", "");
	}
	
	
	@Autowired
	TokenDao tokenDao;
	/**
	 * 根据用户的token获取用户
	 */
	@Override
	public Long getUserIdByToken(String token) {
		Long userId = tokenDao.getUserIdByToken(token);
		return userId;
	}

	@Override
	public R getEdition() {
		return R.ok("请求成功",100).put("data",tokenDao.getEdition());
	}

	@Override
	public R getNewEdition() {
		return R.ok("请求成功",100).put("data",tokenDao.getNewEdition());
	}
}
